package com.example.lib_audio.view

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.constraintlayout.widget.ConstraintLayout
import com.example.lib_audio.activity.MusicPlayActivity
import com.example.lib_audio.audio.AudioController
import com.example.lib_audio.databinding.MusicBottomViewBinding
import com.example.lib_audio.eventbus.AudioChangeProgressEvent
import com.example.lib_audio.eventbus.AudioLoadEvent
import com.example.lib_audio.eventbus.AudioPauseEvent
import com.example.lib_audio.eventbus.AudioRestartRotateAnim
import com.example.lib_audio.eventbus.AudioStartEvent
import com.example.lib_audio.tools.AudioAnimTool
import com.example.lib_audio.tools.ImageTool
import com.example.lib_base.model.Song
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class BottomMusicView(context: Context,attrs:AttributeSet?):ConstraintLayout(context,attrs) {
    private var binding: MusicBottomViewBinding
    private lateinit var audioAnimTool: AudioAnimTool

    init {

        binding = MusicBottomViewBinding.inflate(LayoutInflater.from(context))
        val lp = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, vDp2px(60).toInt())
        //添加子视图
        addView(binding.root,lp)

        audioAnimTool = AudioAnimTool(binding.cover)

        //注册EventBus
        EventBus.getDefault().register(this)

        binding.progressPlayView.setOnClickListener {
            AudioController.getInstance()?.playOrPause()
        }

        binding.textView.setOnClickListener {
            context.startActivity(Intent(context, MusicPlayActivity::class.java))
        }

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onPause(event:AudioPauseEvent){
        audioAnimTool.pause()
        binding.progressPlayView.changeBitmap()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onStart(event:AudioStartEvent){
        audioAnimTool.resume()
        binding.progressPlayView.changeBitmap()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onLoad(event:AudioLoadEvent){
        binding.root.visibility = View.VISIBLE
        initUi(event.song)
        binding.progressPlayView.changeProgress(0f)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun changeProgress(event: AudioChangeProgressEvent){
        val progress = event.current.toFloat() / event.total.toFloat()
        binding.progressPlayView.changeProgress(progress)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun restartAnim(event: AudioRestartRotateAnim){
        audioAnimTool.start(0f,360f) //开启动画
    }


    @SuppressLint("SetTextI18n")
    private fun initUi(song: Song){
        binding.textView.text = "${song.title}-${song.author}"
        ImageTool.loadCircleImage(song.album,binding.cover)
        audioAnimTool.start(0f,360f) //开启动画
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()

        EventBus.getDefault().unregister(this)
    }

    private fun vDp2px(dp: Int) = resources.displayMetrics.density*dp
}